package
{
	import com.AppFacade;
	import com.views.UITop;
	
	import feathers.controls.Button;
	import feathers.controls.Header;
	import feathers.controls.List;
	import feathers.controls.Screen;
	import feathers.data.ListCollection;
	import feathers.themes.MetalWorksMobileTheme;
	
	import flash.events.Event;
	import flash.filesystem.File;
	
	import starling.display.Image;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.textures.Texture;
	import starling.utils.AssetManager;
	
	public class Main extends Sprite
	{
		private var btn : Button;
		private var file : File;
		private var filesPath : String;
		private var filepathList : Array;
		private var assetManager : AssetManager;
		private var namelist : Vector.<String>;
		private var list : List;
		private var listCollection : ListCollection;
		
		public var uitop : UITop;
		
		public function Main()
		{
			this.addEventListener( starling.events.Event.ADDED_TO_STAGE, addedToStageHandler );
		}
		
		private function addedToStageHandler( event:starling.events.Event ):void
		{
			// more here later
			new MetalWorksMobileTheme(this.stage);
			
			uitop = new UITop();
			this.addChild(uitop);
			
			btn = new Button();
			btn.width = 100;
			btn.height = 30;
			btn.label = "选择资源";
//			btn.validate();
			
			list = new List();
			list.width = 250;
			list.height = 300;
			list.y = 40;
			this.addChild( list );
			
			listCollection = new ListCollection();
			
			
			
			assetManager = new AssetManager();
			btn.addEventListener(starling.events.Event.TRIGGERED,onbtnTRIGGERED);
			this.addChild(btn);
			file = new File();
//			
//			AppFacade.Instance().StartUp(this);
		}
		
		private function onbtnTRIGGERED(event : starling.events.Event) : void
		{
			file.browseForDirectory("文件路径");
			file.addEventListener(flash.events.Event.SELECT,selectExportPathOK);
		}
		
		private function selectExportPathOK(e:flash.events.Event):void{
			var file:File = e.target as File;
			file.removeEventListener(flash.events.Event.SELECT,selectExportPathOK);
			filesPath = file.url + "/";
			getfilePath(file);
		}
		
		private function getfilePath(file : File) : void
		{
//			filepathList = new Array();
//			filepathList = file.getDirectoryListing();
//			filepathList[0].name;
			assetManager.enqueue(file);
			assetManager.loadQueue(onprogress);
		}
		
		private function onprogress(ratio:Number) : void
		{
			if(ratio == 1)
			{
				namelist = assetManager.getTextureNames("img_");
				
				var length : int = namelist.length;
				var texture : Texture;
				var obj : Object;
				for(var i : int = 0; i < length; ++i)
				{
					texture = assetManager.getTexture(namelist[i]);
					obj = {iconname : namelist[i], icontexture : texture};
					listCollection.addItem(obj);
//					var img : Image = new Image(texture);
//					img.x = 100 * i;
//					img.y = 40;
//					addChild(img);
					
				}
				list.dataProvider = listCollection;
				list.itemRendererProperties.labelField = "iconname";
				list.itemRendererProperties.iconTextureField = "icontexture";
				
			}
		}
	}
}